<?php
require_once(SERVER_FS_ROOT."/app/_inc/sdao/StructuredDataAccessObject.php");
require_once(SERVER_FS_ROOT."/app/_inc/mix.func.php");
require_once(SERVER_FS_ROOT."/app/util/imex/_inc/importExport.func.php");
/**
 * generreqtype batch del file di release del meta-base
 */
function genMetaBaseTxt($aDataArea = array(), $aModuli=array()) {
	global $objSDAO, $aMetaConPar;
	sxBatchOut("Generreqtype file release del meta-base");
	if (!is_object($objSDAO)) {$objSDAO = new SDAO($aMetaConPar);}
	
	$aExpTab = array();
	
	$sCondDataAreaA = $sCondDataAreaAB = '';
	if (count($aDataArea) > 0) {
		$sCondDataAreaA = " WHERE A.data_area IN ('".implode("','",$aDataArea)."') ";
		$sCondDataAreaAB = $sCondDataAreaA." AND B.data_area IN ('".implode("','",$aDataArea)."') ";
	}
	$sCondModuliA = '';
	if (count($aModuli) > 0) {
		$sCondModuliA = " WHERE A.module IN ('".implode("','",$aModuli)."') ";
	}
	// carica array importreqtype in base alle entity, process_unit, relazioni, menu e librerie presenti nel MB
	$sSQL = "SELECT A.entity FROM entity A $sCondDataAreaA ORDER BY A.entity ";
	$objSDAO->query($sSQL);
	$conta = 0;
	while($row = $objSDAO->fetch()) {
		$conta++;
		$msg = loadExpTab('entity', $row['entity'], $aExpTab );
		if ($msg != '') { sxBatchOut($msg,true); } 
	}
	sxBatchOut("Entita': $conta");
	
	$sSQL = "SELECT A.process_unit FROM process_unit A $sCondModuliA ORDER BY A.process_unit ";
	$objSDAO->query($sSQL);
	$conta = 0;
	while($row = $objSDAO->fetch()) {
		$conta++;
		$msg = loadExpTab('process_unit', $row['process_unit'], $aExpTab );
		if ($msg != '') { sxBatchOut($msg,true); } 
	}
	sxBatchOut("Processi: $conta");
	
	$sSQL = "SELECT R.* FROM relationship R " .
			" INNER JOIN entity A ON A.entity = R.parent_entity " .
			" INNER JOIN entity B ON B.entity = R.child_entity " .
			$sCondDataAreaAB.
			" ORDER BY R.parent_entity, R.child_entity, R.number ";
	$objSDAO->query($sSQL);
	$conta = 0;
	while($row = $objSDAO->fetch()) {
		$conta++;
		if (isset($aExpTab['entity'][$row['parent_entity']])
		and isset($aExpTab['entity'][$row['child_entity']])) {
			$msg = loadExpTab('relationship', $row['parent_entity'].'@'.$row['child_entity'].'@'.$row['number'], $aExpTab );
			if ($msg != '') { sxBatchOut($msg,true); } 
		}
	}
	sxBatchOut("Relazioni: $conta");
	
	$sSQL = "SELECT A.* FROM menu_page A $sCondModuliA ORDER BY A.module, A.menu_page";
	$objSDAO->query($sSQL);
	$conta = 0;
	while($row = $objSDAO->fetch()) {
		$conta++;
		$msg = loadExpTab('menu_page', $row['module'].'@'.$row['menu_page'], $aExpTab );
		if ($msg != '') { sxBatchOut($msg,true); } 
	}
	sxBatchOut("Pagine menu': $conta");
	
	$sSQL = "SELECT * FROM library ORDER BY library";
	$objSDAO->query($sSQL);
	$conta = 0;
	while($row = $objSDAO->fetch()) {
		$conta++;
		$msg = loadExpTab('library', $row['library'], $aExpTab );
		if ($msg != '') { sxBatchOut($msg,true); } 
	}
	sxBatchOut("Librerie funzioni: $conta");
	foreach($aExpTab as $tabRef => $aTabRef) {
			sxBatchOut($tabRef." : ".count($aTabRef));
	}
	sxBatchOut("File meta-base.txt generato correttamente");
	// crea nome file e header file
	$sFileHeader = str_pad("POLARIS-MB ".date("Y-m-d H:i:s")." ".$objSDAO->dbcPar['dbcType']." ".
		$objSDAO->dbcPar['host']." ".$objSDAO->dbcPar['dbName']." ".
		" batch-process xxx",128);
	$serTab = serialize($aExpTab);
	return $sFileHeader.$serTab;
}

function sxBatchOut($mess,$abend=false) {
	if ($abend) {
		echo date('H:i:s')." - *********************************************************************\r\n";
		echo date('H:i:s')." - * ".$mess."\r\n";
		echo date('H:i:s')." - *********************************************************************\r\n";
		echo date('H:i:s')." - "._t("TERMINAZIONE ANOMALA !")."\r\n";
		exit(1);
	}
	echo date('H:i:s')." - ".$mess."\r\n";
}
?>
